//
//  WBPhotoCollectionController.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/22.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "WBPhotoCollectionController.h"
#import "WBPhotoCollectionCell.h"
#import "HMImagePickerController.h"

@interface WBPhotoCollectionController ()<HMImagePickerControllerDelegate , WBPhotoCollectionCellDelegate>




@end

static NSInteger photoCount = 3;

static WBPhotoCollectionController * _instance;

static CGFloat photoMargin = 4;

@implementation WBPhotoCollectionController

static NSString * const reuseIdentifier = @"Cell";

+(instancetype)shared{

    if (nil == _instance) {
        
        CGFloat itmeWidth = (kUISCRRENW - photoMargin * 4) / 3;
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        
        _instance = [[self alloc]initWithCollectionViewLayout:flowLayout];
        
        flowLayout.itemSize = CGSizeMake(itmeWidth, itmeWidth);
        flowLayout.sectionInset = UIEdgeInsetsMake(photoMargin, photoMargin, 0, photoMargin);
        
        flowLayout.minimumLineSpacing = photoMargin;
        flowLayout.minimumInteritemSpacing = photoMargin;
        
        
    }
    
    return _instance;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.collectionView registerClass:[WBPhotoCollectionCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark <UICollectionViewDataSource>


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    NSInteger count = (self.images.count == photoCount) ? 0 : 1;
    return self.images.count + count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WBPhotoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor cz_randomColor];
    
    if (self.images.count == indexPath.item) {
        
        cell.image = nil;
    }else{
    
        cell.image = self.images[indexPath.item];
    }
    
    
    cell.delegate = self;
    
    return cell;
}

#pragma mark <WBPhotoCollectionCellDelegate>

-(void)photoChooseAddClick:(WBPhotoCollectionCell *)cell{

    [self clickSelectPhotoButton];
}

-(void)photoChooseRemoveClick:(WBPhotoCollectionCell *)cell{

    NSIndexPath *indexPath = [self.collectionView indexPathForCell:cell];
    
    [self.images removeObjectAtIndex:indexPath.item];
    [self.selectedAssets removeObjectAtIndex:indexPath.item];
    
    [self.collectionView reloadData];
    
    //没有图片的时候就隐藏图片视图
    self.collectionView.hidden = self.images.count == 0;
}


-(void)clickSelectPhotoButton{

    HMImagePickerController *picker = [[HMImagePickerController alloc] initWithSelectedAssets:self.selectedAssets];
    
    // 设置图像选择代理
    picker.pickerDelegate = self;
    // 设置目标图片尺寸
    picker.targetSize = CGSizeMake(600, 600);
    // 设置最大选择照片数量
    picker.maxPickerCount = photoCount;
    
    [self presentViewController:picker animated:YES completion:nil];
}

#pragma mark - HMImagePickerControllerDelegate
- (void)imagePickerController:(HMImagePickerController *)picker
      didFinishSelectedImages:(NSArray<UIImage *> *)images
               selectedAssets:(NSArray<PHAsset *> *)selectedAssets {
    
    // 记录图像，方便在 CollectionView 显示
    self.images = images.mutableCopy;
    // 记录选中资源集合，方便再次选择照片定位
    self.selectedAssets = selectedAssets.mutableCopy;
    
    [self.collectionView reloadData];
    
    [self dismissViewControllerAnimated:YES completion:nil];
}



@end
